<template>
  <el-card style="margin: 10px 15px">
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          style="width: 240px"
          v-model="categoryStore.c1Id"
          :disabled="categoryStore.isDisabled"
        >
          <el-option
            v-for="category in categoryStore.c1Data"
            :key="category.id"
            :label="category.name"
            :value="category.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          style="width: 240px"
          v-model="categoryStore.c2Id"
          :disabled="categoryStore.isDisabled"
        >
          <el-option
            v-for="category in categoryStore.c2Data"
            :key="category.id"
            :label="category.name"
            :value="category.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          style="width: 240px"
          v-model="categoryStore.c3Id"
          :disabled="categoryStore.isDisabled"
        >
          <el-option
            v-for="category in categoryStore.c3Data"
            :key="category.id"
            :label="category.name"
            :value="category.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, watch } from "vue";
import useCategoryStore from "../../store/modules/category";

const categoryStore = useCategoryStore();

onMounted(() => {
  categoryStore.getC1();
});
watch(
  () => categoryStore.c1Id,
  () => {
    // console.log(typeof categoryStore.c1Id);
    // 有可能是number或是string，我断言这就是number
    if (categoryStore.c1Id !== "") {
      categoryStore.getC2(categoryStore.c1Id as number);
      categoryStore.c2Id = "";
      categoryStore.c3Id = "";
      categoryStore.c3Data = [];
    }
  }
);
watch(
  () => categoryStore.c2Id,
  () => {
    if (categoryStore.c2Id !== "") {
      categoryStore.getC3(categoryStore.c2Id as number);
      categoryStore.c3Id = "";
    }
  }
);
</script>

<style lang="scss" scoped></style>
